package com.dimong.www.loan.utils;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressLint("SimpleDateFormat")
public class ToolUtil {
    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^((\\+{0,1}86){0,1})1[0-9]{10}"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

    /**
     * 昨天，前天，日期，几分前，几小时前
     *
     * @param time
     * @return
     */
    public static String getTime(long time) {
        Date date = new Date(time * 1000);
        long currentTime = System.currentTimeMillis() / 1000;
        Date currentDate = new Date(currentTime * 1000);
        SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd");
        String str = null;
        if (time == 0) {
        } else if (currentTime - time < 60) {
            str = "刚刚";
        } else if (currentTime - time < 3600) {
            str = (currentTime - time) / 60 + "分钟前";
        } else if (currentTime - time < 86400) {
            str = (currentTime - time) / 3600 + "小时前";
        } else if (currentDate.getDate() - date.getDate() == 1 && currentTime - time < 172800) {
            str = "昨天";
        } else if (currentDate.getDate() - date.getDate() == 2 && currentTime - time < 259200) {
            str = "前天";
        } else {
            str = sfd.format(date);
        }
        return str;
    }

    /**
     * 下载app apk
     *
     * @param url
     */
    public static void startInstallApp(Context ctx, String url) {
        DownloadManager downloadManager = (DownloadManager) ctx.getSystemService(ctx.DOWNLOAD_SERVICE);

        Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        //设置允许使用的网络类型，这里是移动网络和wifi都可以
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);

        //禁止发出通知，既后台下载，如果要使用这一句必须声明一个权限：android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
        //request.setShowRunningNotification(false);

        //不显示下载界面
        request.setVisibleInDownloadsUi(false);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setMimeType("application/vnd.android.package-archive");
        /*设置下载后文件存放的位置,如果sdcard不可用，那么设置这个将报错，因此最好不设置如果sdcard可用，下载后的文件        在/mnt/sdcard/Android/data/packageName/files目录下面，如果sdcard不可用,设置了下面这个将报错，不设置，下载后的文件在/cache这个  目录下面*/
        //request.setDestinationInExternalFilesDir(ctx, null, "LiVideoAndroid_"+newVersion+".apk");
        request.setDestinationInExternalPublicDir("QiLoan", "QiLoan" + ".apk");
        long id = downloadManager.enqueue(request);
        Toast.makeText(ctx, "开始下载", Toast.LENGTH_SHORT).show();
    }

    /**
     * 字符串转数字
     *
     * @param str
     * @return
     */
    public static int passInt(String str) {
        if (TextUtils.isEmpty(str)) {
            return 0;
        } else {
            return Integer.parseInt(str);
        }
    }

    /**
     * 字符串转数字并加
     *
     * @param str  字符串
     * @param plus 需要加的值
     * @return
     */
    public static String passIntPlus(String str, int plus) {
        int a;
        try {
            a = passInt(str);
        } catch (Exception e) {
            return str;
        }
        if (a + plus >= 10000) {
            return "1万";
        }
        return String.valueOf(a + plus);
    }

    /**
     * 字符串转数字并加
     *
     * @param str  字符串
     * @param plus 需要加的值
     * @return
     */
    public static String passIntPlus1(String str, int plus) {
        int a;
        try {
            a = passInt(str);
        } catch (Exception e) {
            return str;
        }
        return String.valueOf(a + plus);
    }
}
